import numpy as np
from sklearn.linear_model import Ridge
from sklearn.linear_model import SGDRegressor


X = 2 * np.random.rand(100,1)
Y = 4 + 3*X +np.random.randn(100,1)
X_b = np.c_[np.ones((100,1)),X]

"""
class Ridge
    Linear least squares with l2 regularization.

    Minimizes the objective function::

    ||y - Xw||^2_2 + alpha * ||w||^2_2
"""
# 创建Ridge对象，正则项系数设置为0.4 即 ridge_loss = mse+0.4L2
# solver = 'sag'代表随机梯度下降
"""
'sag' uses a Stochastic Average Gradient descent
"""
ridge = Ridge(alpha=0.4,solver='sag')
# Ridge对象中fit函数自动帮我们补全了w0项，因此不需要对X进行扩充
ridge.fit(X,Y)

"""
print(ridge.predict(1.5))
Expected 2D array, got scalar array instead:
array=1.5.
"""
# 预测值y
print(ridge.predict([[1.5]]))
# 截距项w0
print(ridge.intercept_)
# X系数项w1
print(ridge.coef_)




# SGD stands for Stochastic Gradient Descent 随机梯度下降模块
"""
    penalty : {'l2', 'l1', 'elasticnet'}, default='l2' 惩罚项（正则项）
            The penalty (aka regularization term) to be used. Defaults to 'l2'
            which is the standard regularizer for linear SVM models. 'l1' and
            'elasticnet' might bring sparsity to the model (feature selection)
            not achievable with 'l2'.

    alpha : float, default=0.0001 （正则项系数）
        Constant that multiplies the regularization term. The higher the
        value, the stronger the regularization.
        Also used to compute the learning rate when set to `learning_rate` is
        set to 'optimal'.
    
    fit_intercept : bool, default=True  （是否计算截距项w0）
        Whether the intercept should be estimated or not. If False, the
        data is assumed to be already centered.

    max_iter : int, default=1000  （最大迭代系数）
        The maximum number of passes over the training data (aka epochs).
        It only impacts the behavior in the ``fit`` method, and not the
        :meth:`partial_fit` method.

"""
sgd_reg = SGDRegressor(penalty="l2",alpha=0.1,max_iter=10000)
# SGDRegressor中fit函数要求y为一维向量 numpy.ravel()讲数组转换为一维向量
sgd_reg.fit(X,Y.ravel())
print("sgd_reg.predict:",sgd_reg.predict([[1.5]]))
print("sgd_reg.intercept_:",sgd_reg.intercept_)
print("sgd_reg.coef_:",sgd_reg.coef_)